package com.eleadmin.store.controller;

import com.eleadmin.damon.utils.results.PageResult2;
import com.eleadmin.common.core.web.*;
import com.eleadmin.common.core.annotation.OperationLog;
import com.eleadmin.store.entity.EntryBill;
import com.eleadmin.store.service.EntryBillService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.apache.poi.ss.formula.functions.T;


/**
 *
 * 入库单
 * @author : Doman
 * @date : 2024-02-14 14:59:01
 * @version ：1.0
 */

@Api(tags = "入库单")
@RestController
@RequestMapping("/api/store/entryBill")
public class EntryBillController extends BaseController {

    @Autowired
    private EntryBillService entryBillService;

    @PreAuthorize("hasAuthority('store:entryBill:list')")
    //@ApiOperation("分页查询入库单")
    @GetMapping("/page")
    public JsonResult page(PageResult2<T> page, EntryBill entryBill) throws Exception{
        return entryBillService.list(page, entryBill, getLoginUser());
    }

    @PreAuthorize("hasAuthority('store:entryBill:list')")
    //@ApiOperation("查询全部入库单")
    @GetMapping("entryBillNums")
    public JsonResult entryBillNums(EntryBill entryBill){
        return entryBillService.entryBillNums(entryBill, getLoginUser());
    }

    @PreAuthorize("hasAuthority('store:entryBill:list')")
    //@ApiOperation("查询全部入库单")
    @GetMapping()
    public JsonResult list(EntryBill entryBill){
        return entryBillService.list(entryBill, getLoginUser());
    }

    @PreAuthorize("hasAuthority('store:entryBill:list')")
    //@ApiOperation("根据id查询入库单")
    @GetMapping("/{id}")
    public JsonResult get(@PathVariable("id") String id) {
        return entryBillService.getById(id);
    }


    @PreAuthorize("hasAuthority('store:entryBill:save')")
    @OperationLog
    @ApiOperation(value ="添加入库单")
    @PostMapping()
    public JsonResult save(@RequestBody EntryBill entryBill) {
        try {
            return entryBillService.save(entryBill, getLoginUser());
        } catch (Exception e) {
            return JsonResult.error(e.getMessage());
        }

    }

    @PreAuthorize("hasAuthority('store:entryBill:update')")
    @OperationLog
    @ApiOperation(value ="修改入库单")
    @PutMapping()
    public JsonResult update(@RequestBody EntryBill entryBill) {
        try {
            return entryBillService.update(entryBill, getLoginUser());
        } catch (Exception e) {
            return JsonResult.error(e.getMessage());
        }
    }

    @PreAuthorize("hasAuthority('store:entryBill:update')")
    @OperationLog
    @ApiOperation(value ="修改付款,开票状态")
    @PutMapping("updateTableState")
    public JsonResult updateTableState(@RequestBody EntryBill entryBill) {
        try {
            return entryBillService.updateTableState(entryBill, getLoginUser());
        } catch (Exception e) {
            return JsonResult.error(e.getMessage());
        }
    }

    @PreAuthorize("hasAuthority('store:entryBill:remove')")
    @OperationLog
    @ApiOperation(value ="删除入库单")
    @DeleteMapping("/{id}")
    public JsonResult remove(@PathVariable("id") String id) {
        try {
            return entryBillService.remove(id, getLoginUser());
        } catch (Exception e) {
            return JsonResult.error(e.getMessage());
        }
    }
    @PreAuthorize("hasAuthority('store:entryBill:remove')")
    @OperationLog
    @ApiOperation(value ="删除入库单")
    @DeleteMapping("removeEntryAndSon/{id}")
    public JsonResult removeEntryAndSon(@PathVariable("id") String id) {
        try {
            return entryBillService.removeEntryAndSon(id, getLoginUser());
        } catch (Exception e) {
            return JsonResult.error(e.getMessage());
        }
    }

}
